package com.zzyk.microCloud.api.controller;

import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.Company;
import com.zzyk.main.model.pojo.SysUser;
import com.zzyk.main.model.vo.Message;
import com.zzyk.main.model.vo.OrganizationInfo;
import com.zzyk.main.model.vo.UserInfo;
import com.zzyk.main.model.vo.WxUser;
import com.zzyk.main.service.CompanyService;
import com.zzyk.main.service.ISysOrganizationService;
import com.zzyk.main.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author wss_PC
 */
@RestController
@RequestMapping("/api/company")
public class ApiCompanyController extends ApiBaseController {

    @Resource
    private CompanyService companyService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private ISysOrganizationService organizationService;
    @GetMapping("/getAllBayParam")
    public Message getAllByLike(String param) throws UserException {
        WxUser user = getUser();
        OrganizationInfo organizationInfo = organizationService.get(user.getUserInfo().getEnterprise());
        return Message.success().addData("data",organizationService.getAllByParam(organizationInfo.getArea(),param));
    }
    @RequestMapping("getCompanyById")
    public Company getCompanyById() throws UserException {
        SysUser userInfo=getUser().getSysUser();
        return companyService.selectByPrimaryKey(userInfo.getEnterprise());
    }

    @RequestMapping("/save")
    @Transactional
    public Message save(OrganizationInfo organization) throws UserException {
        UserInfo userInfo=getUser().getUserInfo();
        if (organization == null) {
            throw new UserException("缺少参数");
        }
//        organization.setCounty(ObjectUtil.isNull(organization.getArea()) ? "" : organization.getArea());
        organization.setArea(null);
        if (StringUtils.isNotBlank(organization.getAddress())) {
            String addresse = organization.getAddress().replace(organization.getCity(), "");
            addresse = addresse.replace(organization.getProvince(), "");
            organization.setAddress(addresse);
        }
        if (organization.getId() != null) {
            throw new UserException("请不要传ID");
        } else {
            if (organizationService.get(organization.getName()) != null) {
                throw new UserException("已存在同名组织机构");
            }
            if (organizationService.add(organization) > 0 && organization.getId() != null) {
                return Message.success().addData("data", organization);
            } else {
                throw new UserException("添加组织机构失败");
            }
        }
    }

/*    @Transactional
    public void afterAddOrganization(Organization org, UserInfo userInfo) {
        List<UserInfo> list = sysUserService.getUserList(null, -1);
        list.add(userInfo);
        for (UserInfo user : list) {
            Set<String> area;
            if (StringUtils.isEmpty(user.getArea())) {
                area = new HashSet<>();
            } else {
                area = new HashSet<>(Arrays.asList(user.getArea().split(",")));
            }
            area.add(String.valueOf(org.getId()));
            user.setArea(setAreaString(area));
            sysUserService.updateUser(user);
        }
        List<Organization> organizations = new ArrayList<>();
        organizations.add(organizationService.get(1));
        organizations.add(organizationService.get(userInfo.getEnterprise()));
        for (Organization organization : organizations) {
            Set<String> area;
            if (StringUtils.isEmpty(organization.getArea())) {
                area = new HashSet<>();
            } else {
                area = new HashSet<>(Arrays.asList(organization.getArea().split(",")));
            }
            area.add(String.valueOf(org.getId()));
            organization.setArea(setAreaString(area));
            organizationService.update(organization);
        }

    }*/

    private String setAreaString(Set<String> area) {
        StringBuffer buffer = new StringBuffer();
        for (String str : area) {
            buffer.append(str).append(",");
        }
        buffer.deleteCharAt(buffer.length() - 1);
        return buffer.toString();
    }

}
